package com.ruicar.afs.cloud.afscase.channel.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruicar.afs.cloud.afscase.channel.entity.ChannelWitnessInfo;
import com.ruicar.afs.cloud.afscase.channel.service.ChannelWitnessInfoService;
import com.ruicar.afs.cloud.afscase.channel.utils.BlackDic;
import com.ruicar.afs.cloud.afscase.channel.utils.Common;
import com.ruicar.afs.cloud.common.core.security.util.SecurityUtils;
import com.ruicar.afs.cloud.common.core.util.IResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.util.Date;
import java.util.List;

@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("/witness")
@Api(value = "ChannelWitnessInfo", description = "案件见证人板块")
public class ChannelWitnessInfoController {

    private ChannelWitnessInfoService channelWitnessInfoService;

    @Transactional
    @PostMapping("/save")
    @ApiOperation(value = "保存案件见证人数据")
    public IResponse save(@RequestBody ChannelWitnessInfo witness) throws ParseException {
        ChannelWitnessInfo c = channelWitnessInfoService.getById(witness.getId());
        if (c == null) {
            channelWitnessInfoService.save(witness);
        } else {
            channelWitnessInfoService.updateById(witness);
        }
        return IResponse.success("成功！");
    }

    @ApiOperation(value = "通过身份证号查询见证人信息")
    @RequestMapping(value = "/getWitnessUser", method = RequestMethod.POST)
    @ResponseBody
    @Transactional
    public IResponse getWitnessUser(@RequestBody ChannelWitnessInfo witness) {
        if (witness.getWitnessIdCard() == null || witness.getWitnessIdCard() == "") {
            return IResponse.fail("请输入身份证号码！");
        }
        // 先根据id 查这个信息
        List<ChannelWitnessInfo> list = channelWitnessInfoService.list(Wrappers.<ChannelWitnessInfo>query().lambda().eq(ChannelWitnessInfo::getWitnessIdCard, witness.getWitnessIdCard()));
        if (list.size() > 0) {
            return new IResponse<List<ChannelWitnessInfo>>().setMsg("通过身份证号查询成功").setData(list);
        } else {
            return IResponse.fail("未查询到信息！");
        }
    }


    @PostMapping(value = "/upStatusWitnessByChannelIds")
    @ApiOperation(value = "黑名单根据id停用见证人")
    public IResponse upStatusWitnessByChannelIds(@RequestBody List<Long> ids) {
        //截取字符串

        if (ids.size() > 0) {
            for (int j = 0; j < ids.size(); j++) {
                ChannelWitnessInfo channel = channelWitnessInfoService.getById(ids.get(j));
                if (channel != null) {
                    channel.setStatus(BlackDic.STATUS_STOP);
                    channel.setUpdateTime(new Date(System.currentTimeMillis()));
                    channel.setUpdateBy(SecurityUtils.getUsername());
                    boolean result = channelWitnessInfoService.updateById(channel);
                    if (!result) {
                        return IResponse.fail("状态更新失败！");
                    }
                }
            }
        }
        return new IResponse<Boolean>().setMsg("状态更新成功");
    }

    @PostMapping(value = "/upStatusWitnessByChannelIdsPass")
    @ApiOperation(value = "黑名单根据id启用见证人")
    public IResponse upStatusWitnessByChannelIdsPass(@RequestBody List<Long> ids) {
        //截取字符串
        if (ids.size() > 0) {
            for (int j = 0; j < ids.size(); j++) {
                ChannelWitnessInfo channel = channelWitnessInfoService.getById(ids.get(j));
                if (channel != null) {
                    channel.setStatus(BlackDic.STATUS_PASS);
                    channel.setUpdateTime(new Date(System.currentTimeMillis()));
                    channel.setUpdateBy(SecurityUtils.getUsername());
                    boolean result = channelWitnessInfoService.updateById(channel);
                    if (!result) {
                        return IResponse.fail("状态更新失败！");
                    }
                }
            }
        }
        return new IResponse<Boolean>().setMsg("状态更新成功");
    }

    @PostMapping(value = "/getWitnessByChannelId")
    @ApiOperation(value = "根据渠道id获取见证人")
    public IResponse getWitnessByChannelId(@RequestBody ChannelWitnessInfo witness) {
        List<ChannelWitnessInfo> channelWitnessInfoList = channelWitnessInfoService.list(Wrappers.<ChannelWitnessInfo>query().lambda()
                .eq(ChannelWitnessInfo::getChannelId, witness.getChannelId())
                .eq(ChannelWitnessInfo::getStatus, Common.WITNESS_STATUS_ENABLE)
        .eq(ChannelWitnessInfo::getBusinessAttributes,witness.getBusinessAttributes()));
        return IResponse.success(channelWitnessInfoList);
    }


    @PostMapping("/getWitness")
    @ApiOperation(value = "根据id 查询见证人信息")
    public IResponse<ChannelWitnessInfo> getWitness(@RequestBody ChannelWitnessInfo witness) throws ParseException {
        ChannelWitnessInfo c = channelWitnessInfoService.getById(witness.getId());
        return IResponse.success("成功！").setData(c);
    }

    @PostMapping("/getAllWitness")
    @ApiOperation(value = "查询所有启用的见证人")
    public IResponse getAllWitness() throws ParseException {
        List<ChannelWitnessInfo> c = channelWitnessInfoService.list(Wrappers.<ChannelWitnessInfo>query().lambda()
                .eq(ChannelWitnessInfo::getStatus, Common.WITNESS_STATUS_ENABLE));
        return IResponse.success("成功！").setData(c);
    }

}
